import { loginByUsername, logout, getUserInfo } from '@/api/login'
import { doPost, doGet } from '@/api'
import { getToken, setToken, removeToken } from '@/utils/auth'
import {Message} from "element-ui";

const user = {
  state: {
    user: '',
    status: '',
    code: '',
    token: getToken(),
    name: '',
    fullName: '',
    avatar: '',
    introduction: '',
    roles: [],
    perms: -1,
    news: 0,
    setting: {
      articlePlatform: []
    }
  },
  mutations: {
    SET_CODE: (state, code) => {
      state.code = code
    },
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_INTRODUCTION: (state, introduction) => {
      state.introduction = introduction
    },
    SET_SETTING: (state, setting) => {
      state.setting = setting
    },
    SET_STATUS: (state, status) => {
      state.status = status
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_FULL: (state, name) => {
      state.fullName = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_PERMS: (state, perms) => {
      state.perms = perms
    },
    SET_NEWS: (state, news) => {
      state.news = news
    }
  },

  actions: {
    // 用户名登录
    LoginByUsername({ commit }, userInfo) {
      const username = userInfo.userName.trim()
      return new Promise((resolve, reject) => {
        doGet('login.html?op=login', userInfo).then(response => {
          if (response.status == 0) {
            user.state.status = '0'
            resolve(response)
          } else {
            reject(response)
          }
        }).catch(error => {
          reject(error)
        })
      })
    },
// 获取用户信息
    /*GetNews({ commit, state }) {
      return new Promise((resolve, reject) => {
        doPost('inmsg.html?op=list&pageNum=1&pageSize=20&isRead=0', {}).then(response => {
          const data = response.data
          commit('SET_NEWS',data.totalcount)
          resolve(response)
        }).catch(error => {
          resolve(error)
        })
      })
    },*/
    // 获取用户信息
    GetUserInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        // commit('SET_PERMS', 0)
        // commit('SET_ROLES', 0)
        // commit('SET_NAME', "admin")
        // commit('SET_AVATAR', '')
        // commit('SET_INTRODUCTION', "testData")
        // resolve()

        doPost('personal.html?op=info', {}).then(response => {
          const data = response.data
          // if (data.perms && data.perms.length > 0) { // 验证返回的perms是否是一个非空数组
          //   commit('SET_PERMS', data.perms)
          // } else {
          //   reject('getInfo: perms must be a non-null array !')
          // }
          commit('SET_PERMS', data.userType-0)
          commit('SET_FULL', data.fullName)
          commit('SET_STATUS', response.status)
          commit('SET_ROLES', data.userType-0)
          commit('SET_NAME', data.nickname||data.realName||data.userName)
          commit('SET_AVATAR', data.avatar)
          commit('SET_INTRODUCTION', data.introduction)
          resolve(response)
        }).catch(error => {
          reject(error.message)
        })
      })
    },

    // 第三方验证登录
    // LoginByThirdparty({ commit, state }, code) {
    //   return new Promise((resolve, reject) => {
    //     commit('SET_CODE', code)
    //     loginByThirdparty(state.status, state.email, state.code).then(response => {
    //       commit('SET_TOKEN', response.data.token)
    //       setToken(response.data.token)
    //       resolve()
    //     }).catch(error => {
    //       reject(error)
    //     })
    //   })
    // },

    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        doPost('login.html?op=signOut', {}).then(response => {
          user.state.status = '-1'
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          commit('SET_PERMS', -1)
          removeToken()
          resolve()
        }).catch(error => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          commit('SET_PERMS', -1)
          removeToken()
          resolve()
        })
        // logout(state.token).then(() => {
        //   commit('SET_TOKEN', '')
        //   commit('SET_ROLES', [])
        //   commit('SET_PERMS', [])
        //   removeToken()
        //   resolve()
        // }).catch(error => {
        //   reject(error)
        // })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        // commit('SET_TOKEN', '')
        // removeToken()
        resolve()
      })
    },

    // 动态修改权限
    ChangeRoles({ commit, dispatch }, role) {
      return new Promise(resolve => {
        commit('SET_TOKEN', role)
        setToken(role)
        getUserInfo(role).then(response => {
          const data = response.data
          commit('SET_ROLES', data.roles)
          commit('SET_PERMS', data.perms)
          commit('SET_NAME', data.name)
          commit('SET_AVATAR', data.avatar)
          commit('SET_STATUS', response.status)
          commit('SET_INTRODUCTION', data.introduction)
          dispatch('GenerateRoutes', data) // 动态修改权限后 重绘侧边菜单
          resolve()
        })
      })
    }
  }
}

export default user
